package com.shop.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Component;

import com.shop.entity.Collect;
import com.shop.entity.Product;
import com.shop.entity.Shop;
import com.shop.entity.vo.PageSet;
import com.shop.service.CollectService;

@SuppressWarnings({ "unchecked", "rawtypes" })
@Component
public class CollectServiceImpl extends BaseServiceImpl implements
		CollectService {

	@Override
	public int followProduct(Collect collect) throws Exception {
		Session session = this.openSession();
		Transaction tx = session.beginTransaction();
		Product product = new Product();
		Map<String, Object> p = new HashMap<String, Object>();
		// get prodId
		int prod_id = collect.getProduct().getProdId();
		int cust_id = collect.getCustId();
		p.put("prodId", prod_id);
		p.put("custId", cust_id);
		// check whether exists the collect record or not
		try {
			if (!this
					.find("from Collect c inner join c.product p where p.prodId = :prodId and c.custId = :custId",
							p).isEmpty())
				return 202;
			else {
				// add a new collect record
				p.remove("custId");
				List list = this.find(
						"from Product p where p.prodId = :prodId", p);
				product = (Product) list.get(0);
				collect.setCollectDate(new Date());
				collect.setCollectType("product");
				collect.setProduct(product);
				this.save(collect);
			}
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null)
				tx.rollback();
			return 404;
		} finally {
			session.clear();
			session.close();
		}
		return 200;
	}

	@Override
	public int unfollowProduct(int prod_id, int custId) throws Exception {
		Session session = this.openSession();
		Transaction tx = session.beginTransaction();
		Collect collect = new Collect();
		Map<String, Object> product = new HashMap<String, Object>();
		try {
			product.put("prodId", prod_id);
			product.put("custId", custId);
			List list = this
					.find("From Collect c inner join c.product s where s.prodId = :prodId and c.custId = :custId",
							product);
			// check whether exists the collect record or not
			if (list.isEmpty()) {
				return 204;
			} else {
				// delete corresponding collect record
				for (Object object : list) {
					Object[] obj = (Object[]) object;
					collect = (Collect) obj[0];
					this.delete(collect);
				}
			}
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			return 404;
		} finally {
			session.clear();
			session.close();
		}
		return 200;
	}

	@Override
	public List<Collect> getProdCollectPage(int cust_id, String pageIndex,
			int pageSize) throws Exception {

		String hql = "From Collect cl where cust_id = '"
				+ cust_id
				+ "' and cl.collectType = 'product' order by cl.collectDate desc ";

		PageSet<?> result = (PageSet<?>) this.findAllRecordAndDivPage(pageSize,
				pageIndex, hql);

		return (List<Collect>) result.getList();

		/*
		 * Map<String, Object> cust = new HashMap<String, Object>();
		 * cust.put("custId", cust_id); String hql =
		 * "HQM:select ps.picUrl as picUrl,prod.prodName as prodName,prod.prodPrice as prodPrice,prod.prodTotalscore as prodTotalscore, prod.prodSell as prodSell,prod.prodId as prodId,prod.prodUuid as prodUuid,prom.promPrice as promPrice,prom.promDiscount as promDiscount from Collect c inner join c.product prod inner join prod.pictureRelates p inner join p.pictures ps inner join prod.promotion prom "
		 * + "where c.custId = :custId"; try { List<Map<String, Object>> result
		 * = this.find(hql, pageIndex, pageSize, cust);
		 * System.out.println(result.size() + "size"); return result; } catch
		 * (Exception e) { e.printStackTrace(); return null; }
		 */
	}

	@Override
	public List<Collect> getShopCollectPage(int cust_id, String pageIndex,
			int pageSize) throws Exception {

		String hql = "From Collect cl where cust_id = '" + cust_id
				+ "' and cl.collectType = 'shop' order by cl.collectDate desc";

		PageSet<?> result = (PageSet<?>) this.findAllRecordAndDivPage(pageSize,
				pageIndex, hql);

		return (List<Collect>) result.getList();
	}

	public boolean judgmentDate(Date end) throws Exception {
		Date date = new Date();
		long dif = date.getTime() - end.getTime();
		if (dif < 0) {
			return false;
		}
		double result = dif * 1.0 / (1000 * 60 * 60);
		if (result <= 168) {
			return true;
		} else {
			return false;
		}
	}
}
